/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ClientsOfSOAP;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.json.impl.provider.entity.JSONObjectProvider;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.json.simple.JSONObject;
import serviceofsoap.ShoppingCartSoap;
import serviceofsoap.ShoppingCartSoapImplService;
import shoppingClient.ClientController;
import shoppingClient.Product;
import shoppingClient.ShoppingClientGUI;
/**
 *
 * @author truongtud
 */
public class ShoppingCartSoapClientControler implements ClientController{
    static final String REST_URI ="http://localhost:9990/ebay";
    static final String ADD_USER="/shoppingcart/addUser";
    static final String ALL_PRODUCTS="/shoppingcart/getAllProduct";
    static final String ADD_PRODUCT="/shoppingcart/addProduct";
    static final String REMOVE_PRODUCT="/shoppingcart/removeProduct";
    static final String CHECK_OUT="/shoppingcart/checkOut";
    static final String SUB="/shoppingcart/sub";
    static ShoppingClientGUI clientFrame ;
    private HashMap<String, Product> allItem;
    Client client;
    WebResource service;
    static ShoppingCartSoap shoppingCart;
    public ShoppingCartSoapClientControler(){
        ShoppingCartSoapImplService soapService = new ShoppingCartSoapImplService();
        shoppingCart = soapService.getShoppingCartSoapImplPort();
        clientFrame = new ShoppingClientGUI(this);  
        
		 
        
    }
    public static void main(String[]args){
                      
        ShoppingCartSoapClientControler controller = new ShoppingCartSoapClientControler();
         controller.clientFrame.setVisible(true);
        
                String arr=shoppingCart.getAllProductsText();
		System.out.println(arr);
         
         
    }
    

    @Override
    public void addUser(int id) {
        shoppingCart.addUser(id);
    }

    @Override
    public boolean checkout(int id) {
        String checkoutstand =shoppingCart.checkOutText(id);
        System.out.println("Luc checkout"+checkoutstand);
        if(checkoutstand.compareTo("true")==0)
            return true;
        else 
              return false;
    }

    @Override
    public HashMap<String, Product> load() {
        allItem = new HashMap<String, Product>();
        String products= shoppingCart.getAllProductsText();
        System.out.println(products);
        String[] productsinfo = products.split(":");
       // System.out.println(productsinfo[0]);
        for(String s : productsinfo){
            System.out.println(s);
            String[] temp = s.split("-");
            Product p = new Product(temp[0], Double.parseDouble(temp[1]), Integer.parseInt(temp[2]));
            allItem.put(temp[0], p);
        }
       return allItem;
    }

    @Override
    public void removeProduct(int id,String name) {
        shoppingCart.removeProduct(id, name);
//       String remove= getOutputAsText(addProduct);
    }

    @Override
    public void addProduct(int UserID ,String name) {
             System.out.println( shoppingCart.addProduct(UserID, name));
    }
    
    
}
